package com.ruoyi.web.controller.train;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.regulatory.domain.JyBureau;
import com.ruoyi.regulatory.service.JyBureauService;
import com.ruoyi.train.domain.JyTrainingSchool;
import com.ruoyi.train.service.IJyTrainingSchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 培训学校基础信息Controller
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/training/school")
public class JyTrainingSchoolController extends BaseController {
    private final IJyTrainingSchoolService jyTrainingSchoolService;
    private final JyBureauService jyBureauService;

    /**
     * 查询培训学校基础信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(JyTrainingSchool jyTrainingSchool) {
        startPage();
        List<JyTrainingSchool> list = jyTrainingSchoolService.selectJyTrainingSchoolList(jyTrainingSchool);

        if (CollectionUtil.isNotEmpty(list)) {
            Map<Long, JyBureau> longJyBureauMap = jyBureauService.selectJyBureauMap();

            list.forEach(v -> {
                Long bureauId = v.getBureauId();
                if (null != bureauId) {
                    JyBureau jyBureau = longJyBureauMap.get(bureauId);
                    if (null != jyBureau) {
                        v.setBureauName(jyBureau.getName());
                    }
                }
            });
        }

        return getDataTable(list);
    }

    /**
     * 导出培训学校基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('basics:training:export')")
    @Log(title = "培训学校基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JyTrainingSchool jyTrainingSchool) throws IOException {
        List<JyTrainingSchool> list = jyTrainingSchoolService.selectJyTrainingSchoolList(jyTrainingSchool);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "培训学校基础信息数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JyTrainingSchool.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取培训学校基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('basics:training:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jyTrainingSchoolService.selectJyTrainingSchoolById(id));
    }

    /**
     * 修改培训学校基础信息
     */
    @PreAuthorize("@ss.hasPermi('basics:training:check')")
    @Log(title = "培训学校基础信息", businessType = BusinessType.UPDATE)
    @PostMapping("/check")
    public AjaxResult check(@RequestBody JSONObject body) {
        Long id = body.getLong("id");
        Byte state = body.getByte("state");
        String remarks = body.getString("remarks");

        return toAjax(jyTrainingSchoolService.checkTrainingSchool(id, state, remarks));
    }
}
